﻿using UnityEngine;
using System.Collections;

public class MusicMgr
{
    /// <summary>
    /// 音乐管理游戏单例对象
    /// </summary>
    private GameObject gameObject;
    /// <summary>
    /// 背景音乐(循环播放)
    /// </summary>
    private AudioSource bgAudioSource;
    private static MusicMgr instance;
    public static MusicMgr Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new MusicMgr();
                instance.gameObject = new GameObject("_MusicMgr");
                instance.bgAudioSource = instance.gameObject.AddComponent<AudioSource>();
                instance.bgAudioSource.loop = true;
                Object.DontDestroyOnLoad(instance.gameObject);
            }
            return instance;
        }
    }
    /// <summary>
    /// 是否播放背景音乐
    /// </summary>
    private bool isPlayBg = true;
    public bool IsPlayBg { get { return isPlayBg; } }
    /// <summary>
    /// 是否播放音效
    /// </summary>
    private bool isPlayEffect = true;
    public bool IsPlayEffect { get { return isPlayEffect; } }
    /// <summary>
    /// 开关控制背景音乐
    /// </summary>
    public void ToggleBg(bool isPlayBg)
    {
        this.isPlayBg = isPlayBg;
        if (isPlayBg)
        {
            if (!bgAudioSource.isPlaying)
            {
                bgAudioSource.Play();
            }
        }
        else
        {
            bgAudioSource.Pause();
        }
    }
    /// <summary>
    /// 开关控制音效
    /// </summary>
    public void ToggleEffect(bool isPlay)
    {
        isPlayEffect = isPlay;
    }
    /// <summary>
    /// 设置背景音乐声音大小
    /// </summary>
    public void SetBgVolumn(float volumn)
    {
        bgAudioSource.volume = volumn;
    }

    public AudioClip getAudioClip(string name)
    {
        var audioClip = ResourceManager.LoadResource(name) as AudioClip;
        if (audioClip == null)
        {
            GameLog.WarningFormat("加载音乐资源({0})失败!", name);
        }
        return audioClip;
    }
    /// <summary>
    /// 播放游戏背景音乐
    /// </summary>
    public void PlayBg(string name)
    {
        if (!isPlayBg) return;
        PlayBg(getAudioClip(name));
    }

    /// <summary>
    /// 播放游戏背景音乐
    /// </summary>
    public void PlayBg(AudioClip audioClip)
    {
        if (!isPlayBg) return;
        if (audioClip == null)
        {
            GameLog.Log("PlayBg(AudioClip audioClip)异常,audioClip = null");
            return;
        }
        bgAudioSource.clip = audioClip;
        bgAudioSource.Play();
    }

    /// <summary>
    /// 播放游戏音效
    /// </summary>
    public void PlayEffect(string name)
    {
        if (!isPlayEffect) return;
        PlayEffect(getAudioClip(name));
    }

    /// <summary>
    /// 播放游戏音效
    /// </summary>
    public void PlayEffect(AudioClip audioClip)
    {
        if (!isPlayEffect) return;
        AudioSource audioSource = gameObject.AddComponent<AudioSource>();
        audioSource.clip = audioClip;
        audioSource.Play();
        Object.Destroy(audioSource, audioClip.length);
        GameLog.LogFormat("PlayEffectMusic name = {0}, length = {2}", audioClip.name, audioClip.length);
    }

    /// <summary>
    /// 清空所有正在播放的音效
    /// </summary>
    public void ClearAllEffect()
    {
        var audioSources = gameObject.GetComponents<AudioSource>();
        foreach (var audioSource in audioSources)
        {
            if (audioSource == bgAudioSource) continue;
            Object.Destroy(audioSource);
        }
    }
}
